<?php
namespace app\a\controller;
use app\BaseController;
use think\facade\Db;
use think\exception\HttpResponseException;//引入
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: x-token, x-uid, Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, OPATION');
header('content-type:application/json;charset=utf8');
class Base extends BaseController
{
    /*定义一个初始数组*/
    protected $info = array(
        'code'=>200,//200 正常 10001 验证失败  10002未登录   10003 账号不存在
        'msg'=>'提示',
        'total'=>0,//数据数量
        'data'=>[]
    );

    //写入操作日志
    protected function logData($action='',$admin_id=0){
        $pt['time'] = time();
        $pt['admin_id'] =  $admin_id;
        $pt['action'] =  trim($action);
        Db::table('an_admin_log')->insert($pt);
    }

    protected function initialize(){
        $urlAll = input('param.');

        //判断是否有mrchenanan参数，如果没有，需要进行参数验证
        if(!isset($urlAll['mrchenanan'])){
            $urlTime = $urlAll['time'] + 100;
            $surplusTime = time() - $urlAll['time'];//当前时间 减去 传过来的时间 结果小于10 有效 
    
            //验证参数 请求时间戳必须比当前时间戳小于10秒 且 前端md5加密与后台md5加密数据一致
            if(($surplusTime<10) && (md5($urlTime) == $urlAll['sign'])){
    
                //判断是否登录
                if($urlAll['token'] == ''){
                    $this->redirect(config('an_config.baseUrl').'/api/Login/isNot?code=10002');
                }else{
                    $res = Db::table('an_admin')->where('id', $urlAll['token'])->find();
                    if($res == null){
                        $this->redirect(config('an_config.baseUrl').'/api/Login/isNot?code=10003');
                    }
                }
            }else{
                $this->redirect(config('an_config.baseUrl').'/api/Login/isNot?code=10001');
            }
        }



    }

    // 重定向
    public function redirect(...$arg) {
        throw new \think\exception\HttpResponseException(redirect(...$arg));
    }
}
